using UnityEngine;
using System.Collections;

public class PlayerCollision : MonoBehaviour 
{
	
	private GameObject m_go_OriginalCorpse;
	private CheckpointManager m_checkpointManager;
	private CorpseManager m_corpseManager;
	
	void Start () 
	{
		m_go_OriginalCorpse = GameObject.Find("Corpse");
		m_go_OriginalCorpse.SetActive(false);
		
		GameObject goCheckpointManager= GameObject.Find("CheckpointManager");
		m_checkpointManager = goCheckpointManager.GetComponent<CheckpointManager>();
		
		GameObject goCorpseManager = GameObject.Find("CorpseManager");
		m_corpseManager = goCorpseManager.GetComponent<CorpseManager>();
	}
	
	void Update()
	{
		if(Input.GetKeyDown(KeyCode.A))
			m_corpseManager.deleteAllCorpse();
	}
	
	void OnTriggerEnter(Collider other)
	{
		//Debug.Log("trigger collision " + other.gameObject.name);
		if(other.gameObject.layer == LayerMask.NameToLayer("CorpseKiller"))
		{
			//create body
			GameObject goNewCorpse = (GameObject)Instantiate(m_go_OriginalCorpse);
			goNewCorpse.transform.position = transform.position;
			goNewCorpse.transform.rotation = transform.rotation;
			goNewCorpse.SetActive(true);
			m_corpseManager.addCorpse(goNewCorpse);
			
			//kill the killer
			other.gameObject.SetActive(false);
			
			//respawn
			Respawn();
			
		}
		else if(other.gameObject.layer == LayerMask.NameToLayer("Killer"))
		{
			Respawn();
		}
	}
	
	private void Respawn()
	{
		GameObject Flag = m_checkpointManager.SelectedCheckpoint.gameObject.transform.Find("flag_flag").gameObject;
		transform.position = new Vector3(Flag.transform.position.x, Flag.transform.position.y, 0);//m_checkpointManager.SelectedCheckpoint.transform.position;//goSpawn.transform.position;
		transform.rotation = Quaternion.Euler(new Vector3(0, 90, 0));
			
	}
}
